#!/usr/bin/env bash

PREFIX=$(cd "$(dirname "$0")"; pwd)
cd $PREFIX

source config.sh
DATA_PATH=$BACKUP_PATH/`date +%F`/psql/wxbot

if [ -d $DATA_PATH ]; then
    PSQL="psql -U $PSQL_USER -d $PSQL_DB -h $PSQL_HOST -p $PSQL_PORT"

    function load {
        eval $PSQL '-c "DROP SCHEMA $1 CASCADE;"'
        eval $PSQL '-c "CREATE SCHEMA $1;"'
        eval $PSQL '-f table/$PSQL_DB/$1.sql'
        echo $DATA_PATH/$1.pg_restore
        pg_restore $DATA_PATH/$1.pg_restore | psql -U $PSQL_USER -d $PSQL_DB -h $PSQL_HOST -p $PSQL_PORT
    }
    load public
    load url
    load wx
    load pbody
else
    echo "postgresql 导入数据失败，数据备份不存在"
fi
#DATA=$PREFIX/data/$PSQL/wx.sql
# rm $DATA
# rsync -avP gu321:/mnt/data/backup/psql/ol/data.sql.gz $DATA
# psql -f $PREFIX/data/truncate.sql $PSQL_USER -d $PSQL_DB -h $PSQL_HOST -p $PSQL_PORT

#psql -U $PSQL_USER -h $PSQL_HOST -p $PSQL_PORT -c "DROP SCHEMA wx CASCADE;"
#psql -f $PREFIX/gu321.u88.sql -U $PSQL_USER -d $PSQL_DB -h $PSQL_HOST -p $PSQL_PORT
#psql -f $DATA -U $PSQL_USER -d $PSQL_DB -h $PSQL_HOST -p $PSQL_PORT
